package com.net.oil.order;

import android.os.Bundle;
import
androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.base.BaseActivity;
import com.net.oil.order.adapter.MaintainRecyclerViewAdapter;
import com.net.oil.order.bean.MaintainListBean;
import com.net.oil.order.contract.MaintainListContract;
import com.net.oil.order.presenter.MaintainListPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MaintainListActivity extends BaseActivity<MaintainListPresenter> implements MaintainListContract.View {

    @BindView(R.id.toolbar_go_back)
    ImageView toolbarGoBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.maintain_recycler_view)
    RecyclerView maintainRecyclerView;

    private MaintainRecyclerViewAdapter adapter;

    private int page = 1;           //当前页
    private int rows = 20;          //分页大小
    private boolean haveMore = true;
    private int carId = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maintain_list);
        ButterKnife.bind(this);
        presenter = new MaintainListPresenter(this);
        initData();
        initView();
    }

    private void initData() {
        carId = getIntent().getIntExtra(Constant.INTENT_CAR_ID, -1);
        if (carId != -1){
            showProgressDialog();
            presenter.getMaintainList(carId, page, rows);
        }
    }

    private void initView() {
        toolbarTitle.setText("保养记录列表");
        toolbarGoBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        LinearLayoutManager manager = new LinearLayoutManager(this);
        adapter = new MaintainRecyclerViewAdapter(this);
        maintainRecyclerView.setLayoutManager(manager);
//        maintainRecyclerView.addItemDecoration(new DividerItemDecoration(this, manager.getOrientation()));
        maintainRecyclerView.setAdapter(adapter);

        maintainRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                //如果不能向上滚动，有更多则加载更多
                if (!maintainRecyclerView.canScrollVertically(1) && haveMore && page > 1) {
                    adapter.changeMoreStatus(Constant.LOADING_MORE);
                    presenter.getMaintainList(carId, page, rows);
                }
            }
        });
    }

    /**
     * 获取保养记录列表成功
     * @param maintainListBean  记录列表
     */
    @Override
    public void getMaintainListSuccess(MaintainListBean maintainListBean) {
        dismissProgressDialog();
        if (maintainListBean != null && maintainListBean.getList() != null){
            if (maintainListBean.getList() != null && maintainListBean.getList().size() > 0){
                if (page == 1) {
                    adapter.updateData(maintainListBean.getList());
                } else if (page > 1) {
                    adapter.loadMore(maintainListBean.getList());
                }

                adapter.changeMoreStatus(Constant.PULL_LOAD_MORE);
                haveMore = true;
                page++;
            }else {
                adapter.changeMoreStatus(Constant.LOADING_NONE);
                haveMore = false;
            }
        }
    }

    /**
     * 获取保养记录列表失败
     */
    @Override
    public void getMaintainListFailed() {
        dismissProgressDialog();
    }
}
